package com.mantru.iotpot.job;

import android.os.Message;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.modbus.Common;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 手动炒制管理器
 */
public class HandingMaster {
    private ApplicationClass ac;
    //UI界面上的几个参数值
    //执行状态，0未执行、1执行中、2已完成、3终止了
    public int steep_status;
    //记录当前界面上操作的参数
    public RecordSteepEntity current_entity;
    //录制队列，相关变量
    public Map<Integer, String> record_map;
    public JSONObject record_object;
    //录制状态，0未开始，1录制中，2已结束
    public int record_status;
    //录制时长，13位时间截
    public long record_begin_time, record_end_time;

    //创建
    public HandingMaster(ApplicationClass ac){
        this.ac = ac;
        this.steep_status = 0;  //未执行
        this.current_entity = new RecordSteepEntity();
        this.record_map = null;
        this.record_object = null;
        this.record_status = 0;
        this.record_begin_time = 0;
        this.record_end_time = 0;
        ac.gripper_speed_value = 0;
        ac.gripper_rotation_value = 0;
        try {
            //要判断机械臂在放下位
            //要判断锅处于原位
            //要判断料盒轨处于第1位
            if (ac.gripper_position_value != 1 || ac.pot_position_value != 3 || ac.box_position_value != 1) {
                ac.Cooking_Setting3Position("handmaster");
                return;
            }

            //启动
            Start();

        } catch (Exception ex) {
            ac.SaveLog("HandingMaster初始化出错：" + ex.getMessage());
        }
    }

    //启动
    public void Start() {
        if (this.steep_status != 0)
            return;
        this.steep_status = 1;  //执行中
        SendInit();
    }

    //停止
    public void Stop() {
        steep_status = 3;  //终止了
        //火力0档、机构臂抬起、喷泵全关
        Close();
    }

    //火力0档、机构臂抬起、喷泵全关
    private void Close() {
        try {
            //火力，0档
            ac.com.writeSingleRegister(0x01, 0x00FD, 0x00, null);
            //机械爪，0档
            ac.com.writeSingleRegister(0x01, 0x00E1, 0x00, null);
            //机构臂抬起
            //ac.com.writeSingleRegister(0x01, 0x00DF, 0x02, null);
            //喷泵全关
            int[] nozzle_values = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            ac.com.writeHoldingRegisters(0x01, 0x00F2, nozzle_values.length, nozzle_values);
            nozzle_values = null;
            ac.SaveLog("HandingMaster.Close()了");
        } catch (Exception ex) {
            ac.SaveLog("HandingMaster.Close()出错：" + ex.getMessage());
        }
    }

    //发送初始状态的指令
    private void SendInit() {
        ac.SaveLog("HandingMaster.SendInit()");
        try {
            //锅设置
            //火力，0档
            ac.com.writeSingleRegister(0x01, 0x00FD, 0x00, null);
            //机械臂放下，0档，正向
            ac.GripperSetting(0x01, 0x00, 0x00);
            //调味料喷泵全关
            int[] nozzle_values = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            ac.com.writeHoldingRegisters(0x01, 0x00F2, nozzle_values.length, nozzle_values);
            nozzle_values = null;
            //料盒回归到第1位
            ac.com.writeSingleRegister(0x01, 0x00DE, 0x01, null);
        } catch (Exception ex) {
            ac.SaveLog("HandingMaster.SendInit()出错：" + ex.getMessage());
        }
    }


    //泵控制算法，如果是开的则是左移2位+1
    private int NozzleControl(int nozzle_value)
    {
        if (nozzle_value <= 0)
            return 0;
        int value = nozzle_value << 2 + 1;
        return value;
    }

    //添加一步录制
    //steep_type  步骤类型：1加热设置、2调味料投放、3料盒投放、4手动投放
    public void AddRecording(int steep_type) {
        if (record_status != 1)
            return;
        try {
            long now = Common.GetTimeStamp13();
            //火力和爪子的设置相差2500ms以内的合并为一步
            if (steep_type == 1 && record_map.size() >= 1 && current_entity.steep_type == 1) {
                long sub_time = now - current_entity.last_record_time;
                if (sub_time < 2500){
                    record_map.put(current_entity.steep_index, current_entity.ToString());
                    return;
                }
            }

            long time_long = now - record_begin_time;
            current_entity.time_long = (int) (time_long / 1000);
            if (time_long % 1000 >= 500)
                current_entity.time_long++;
            current_entity.time_value = Common.int2time(current_entity.time_long);
            current_entity.steep_type = steep_type;
            current_entity.steep_index++;
            current_entity.last_record_time = now;
            //投放料盒的要计算一下料盒序号
            if (steep_type == 3) {
                current_entity.box1_name = "";
                current_entity.box2_name = "";
                current_entity.box3_name = "";
                current_entity.box4_name = "";
                current_entity.box1_value = 0;
                current_entity.box2_value = 0;
                current_entity.box3_value = 0;
                current_entity.box4_value = 0;
                current_entity.box_data = "";
                if (current_entity.box_index == 1) {
                    current_entity.box1_name = record_object.getJSONArray("spec_data").getJSONObject(0).getJSONArray("material_data").getJSONObject(0).getString("material_name");
                    current_entity.box1_value = record_object.getJSONArray("spec_data").getJSONObject(0).getJSONArray("material_data").getJSONObject(0).getInt("weight");
                    if (current_entity.box1_name.length() <= 0) current_entity.box1_name = "未知";
                    if (current_entity.box1_value <= 0) current_entity.box1_value = 1;
                    current_entity.box_data = "{\"name\":\""+current_entity.box1_name+"\",\"value\":\""+current_entity.box1_value+"\"}";
                }
                if (current_entity.box_index == 2) {
                    current_entity.box2_name = record_object.getJSONArray("spec_data").getJSONObject(0).getJSONArray("material_data").getJSONObject(1).getString("material_name");
                    current_entity.box2_value = record_object.getJSONArray("spec_data").getJSONObject(0).getJSONArray("material_data").getJSONObject(1).getInt("weight");
                    if (current_entity.box2_name.length() <= 0) current_entity.box2_name = "未知";
                    if (current_entity.box2_value <= 0) current_entity.box2_value = 1;
                    current_entity.box_data = "{\"name\":\""+current_entity.box2_name+"\",\"value\":\""+current_entity.box2_value+"\"}";
                }
                if (current_entity.box_index == 3) {
                    current_entity.box3_name = record_object.getJSONArray("spec_data").getJSONObject(0).getJSONArray("material_data").getJSONObject(2).getString("material_name");
                    current_entity.box3_value = record_object.getJSONArray("spec_data").getJSONObject(0).getJSONArray("material_data").getJSONObject(2).getInt("weight");
                    if (current_entity.box3_name.length() <= 0) current_entity.box3_name = "未知";
                    if (current_entity.box3_value <= 0) current_entity.box3_value = 1;
                    current_entity.box_data = "{\"name\":\""+current_entity.box3_name+"\",\"value\":\""+current_entity.box3_value+"\"}";
                }
                if (current_entity.box_index == 4) {
                    current_entity.box4_name = record_object.getJSONArray("spec_data").getJSONObject(0).getJSONArray("material_data").getJSONObject(3).getString("material_name");
                    current_entity.box4_value = record_object.getJSONArray("spec_data").getJSONObject(0).getJSONArray("material_data").getJSONObject(3).getInt("weight");
                    if (current_entity.box4_name.length() <= 0) current_entity.box4_name = "未知";
                    if (current_entity.box4_value <= 0) current_entity.box4_value = 1;
                    current_entity.box_data = "{\"name\":\""+current_entity.box4_name+"\",\"value\":\""+current_entity.box4_value+"\"}";
                }
            }
            record_map.put(current_entity.steep_index, current_entity.ToString());
        } catch (Exception ex) {
            ac.SaveLog("HandingMaster.AddRecording("+steep_type+")出错：" + ex.getMessage());
        }
    }

}


